package features.advance.leetcode.tree.easy;

import features.advance.leetcode.tree.model.TreeNode;
import features.advance.leetcode.util.TreeUtil;

public class Solution104 {

    int maxAns = 0;
    int ans = 0;

    /**
     * 左子树和右子树最大值加1
     *  maxAns = max(L,R) + 1
     * @param root
     * @param dummy
     * @return
     */
    public int maxDepth(TreeNode root,boolean dummy) {
        if (root == null) {
            return 0;
        } else {
            int leftHeight = maxDepth(root.left);
            int rightHeight = maxDepth(root.right);
            return Math.max(leftHeight, rightHeight) + 1;
        }
    }

    public int maxDepth(TreeNode root) {
        if(root == null){
            return maxAns;
        }
        depth(root);
        return maxAns;
    }

    public void depth(TreeNode root){
        if(root != null){
            ans++;
            if(root.left == null && root.right ==null){
                maxAns = Math.max(maxAns,ans);
            }else{
                depth(root.left);
                ans--;
                depth(root.right);
                ans--;
            }
        }else{
            ans++;
        }
    }

    public static void main(String[] args) {
        /*
                   3
                 /   \
                9     20
              /  \   /  \
             1    2  15   7
           /   \
           1    2
         */
        String str = "[3,9,20,1,2,15,7,1,2,null,null]";
//        str = "[1,null,2]";
        TreeNode treeNode = TreeUtil.parseArray(str);
        int i = new Solution104() {
        }.maxDepth(treeNode);
        System.out.println(i);
    }
}
